﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;

namespace SalesManagementWrite.API.SalesManagementWrites.PurchaseReturnOrderes
{
    public class PurchaseReturnOrderCreateHandler : IRequestHandler<PurchaseReturnOrderCreateCommand, Result>
    {
        private readonly IBaseRepository<PurchaseReturnOrder> _purchaseReturnOrderRepository;
        private readonly IBaseRepository<PurchaseReturnOrderSubtable> _purchaseReturnOrderSubtableRepository;
        private readonly ISqlSugarClient _sqlSugarClient;

        public PurchaseReturnOrderCreateHandler(IBaseRepository<PurchaseReturnOrder> purchaseReturnOrderRepository, IBaseRepository<PurchaseReturnOrderSubtable> purchaseReturnOrderSubtableRepository, ISqlSugarClient sqlSugarClient)
        {
            _purchaseReturnOrderRepository = purchaseReturnOrderRepository;
            _purchaseReturnOrderSubtableRepository = purchaseReturnOrderSubtableRepository;
            _sqlSugarClient = sqlSugarClient;
        }
        /// <summary>
        /// 采购退货单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PurchaseReturnOrderCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            _sqlSugarClient.Ado.BeginTran();
            try
            {
                var purchaseReturnOrder = new PurchaseReturnOrder()
                {
                    PurchaseReturnOrderId = request.PurchaseReturnOrderId,
                    PurchaseReturnOrderNo = request.PurchaseReturnOrderNo,
                    PurchaseReturnOrderSerialNumber = request.PurchaseReturnOrderSerialNumber,
                    BusinessType = request.BusinessType,
                    PurchaseOrderNo = request.PurchaseOrderNo,
                    DescInfo = request.DescInfo,
                    DocumentDate = request.DocumentDate,
                    Operator = request.Operator,
                    Supplier = request.Supplier,
                    DocumentsStatus = request.DocumentsStatus,
                    Audit = request.Audit,
                    ReturnsWarehouse = request.ReturnsWarehouse,
                    BusinessStatus = request.BusinessStatus,
                    AuditDate = request.AuditDate,
                    ReasonForReturn = request.ReasonForReturn,
                };
                var purchaseReturnOrderRes = await _purchaseReturnOrderRepository.Add(purchaseReturnOrder);
                List<PurchaseReturnOrderSubtable> purchaseReturnOrderSubtable = new List<PurchaseReturnOrderSubtable>();
                foreach (var item in request.purchaseReturnOrderSubtableDtos)
                {
                    purchaseReturnOrderSubtable.Add(new PurchaseReturnOrderSubtable()
                    {
                        PurchaseReturnOrderSubtableId = item.PurchaseReturnOrderSubtableId,
                        CopyNo = item.CopyNo,
                        MaterialNo = item.MaterialNo,
                        MaterialName = item.MaterialName,
                        LotNumber = item.LotNumber,
                        Num = item.Num,
                        OutboundShipmentsNum = item.OutboundShipmentsNum,
                        ReturnInstructions = item.ReturnInstructions,
                        PurchaseReturnOrderSerialNumber = purchaseReturnOrder.PurchaseReturnOrderSerialNumber,//流水号外键
                    });
                }
                var purchaseReturnOrderSubtableRes = await _purchaseReturnOrderSubtableRepository.AddRange(purchaseReturnOrderSubtable);
                if (purchaseReturnOrderRes > 0 && purchaseReturnOrderSubtableRes > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                _sqlSugarClient.Ado.CommitTran();
                return result;
            }
            catch (Exception ex)
            {

                _sqlSugarClient.Ado.RollbackTran();
                throw ex;
            }
        }
    }
}
